﻿Function Get-EventGroup
{
  [OutputType('EventGroup[]')]
  [CmdletBinding(DefaultParameterSetName="Number")]
  param
  (
    [parameter(Position=0,
               ParameterSetName="Number")]
    [int]
    $Number = -1,

    [parameter(Position=0,
               ParameterSetName="FriendlyName")]
    [String]
    $FriendlyName,

    [parameter(Position=0,
               ParameterSetName="Order")]
    [int]
    $Order = -1,

    [parameter(Position=0,
               ParameterSetName="Id")]
    [System.Guid]
    $Id
  )

  Begin
  {
  }

  Process
  {
    try
    {
      $DeserializedGroupList = Import-Clixml $Script:GroupXMLPath
    }
    catch [System.IO.FileNotFoundException]
    {
      # If the groups.xml file doesn't exist, create an empty array.
      $DeserializedGroupList = @()
    }

    # Import-Clixml returns deserialized objects, which need to be converted to normal ones.
    $GroupList = @()
    foreach ($DeserializedGroup in $DeserializedGroupList)
    {
      $Group = New-Object EventGroup
      $Group.Number           = $DeserializedGroup.Number
      $Group.Id               = $DeserializedGroup.Id
      $Group.FriendlyName     = $DeserializedGroup.FriendlyName
      $Group.Order            = $DeserializedGroup.Order
      $Group.Description      = $DeserializedGroup.Description
      $Group.FilterConditions = $DeserializedGroup.FilterConditions
      $Group.AssignedHandlers = $DeserializedGroup.AssignedHandlers
      $Group.IsEnabled        = $DeserializedGroup.IsEnabled

      $GroupList += $Group
    }

    # Filter the returned groups by number, length, Id, or order.  Throw an exception if none are found.
    if ($Number -ge 0)
    {
      $GroupList = $($GroupList | Where-Object {$_.Number -eq $Number})
      if ($GroupList -eq $Null)
      {
        $Exception = New-Object System.ArgumentException "A parameter is invalid.  Cannot find an Event Group with number $Number."
        throw $Exception
      }
    }
    if ($FriendlyName.Length -gt 0)
    {
      $GroupList = $($GroupList | Where-Object {$_.FriendlyName -eq $FriendlyName})
      if ($GroupList -eq $Null)
      {
        $Exception = New-Object System.ArgumentException "A parameter is invalid.  Cannot find an Event Group with Friendly Name '$FriendlyName'"
        throw $Exception
      }
    }
    if ($Id -ne $Null)
    {
      $GroupList = $($GroupList | Where-Object {$_.Id -eq $Id})
      if ($GroupList -eq $Null)
      {
        $Exception = New-Object System.ArgumentException "A parameter is invalid.  Cannot find an Event Group with Id '$Id'"
        throw $Exception
      }
    }
    if ($Order -ge 0)
    {
      $GroupList = $($GroupList | Where-Object {$_.Order -eq $Order})
      if ($GroupList -eq $Null)
      {
        $Exception = New-Object System.ArgumentException "A parameter is invalid.  Cannot find an Event Group with Order '$Order'"
        throw $Exception
      }
    }

    $GroupList = $GroupList | Sort-Object Order

    return $GroupList
  }

  End
  {
  }
}